package com.muyangren.poidemo;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.muyangren.poidemo.entity.*;
import com.muyangren.poidemo.utils.word.template.*;
import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.ClassPathResource;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * ps：如果你是刚入门的，建议就别看了，徒增痛苦，告诉leader这需求不行
 */
@SpringBootTest
class PoiDemoApplicationTests2 {


    private static final String OUTPUT_PDF_PATH = "C:\\\\Users\\\\br-22\\\\Desktop\\export-word.pdf";
    private static final String OUTPUT_DOCX_PATH = "C:\\\\Users\\\\br-22\\\\Desktop\\\\export-word.docx";
    private static final String TEMPLATE_PATH = "templates/export-word.docx";

    /**
     * 按照我写的顺序先看一遍
     */
    @Test
    void contextLoads() {
        try {
            // 一、初始化数据
            // 1）初始化模板数据-实际是去数据库查询-自行整合
            Templates templates = initTemplates();
            // 2）初始化家庭成员信息-实际是去数据库查询-自行整合
            List<DataValue> familyMemberList = initFamilyMember();
            // 3）初始化工作情况-实际是去数据库查询-自行整合
            List<Going> goingList = initGoing();

            // 二、初始化动态数据-并整合一个完整的对象
            // 1）处理动态数据->转为RowRenderData类型即List<Going>->List<RowRenderData>
            TemplateRowRenderData templateRowRenderData = new TemplateRowRenderData(familyMemberList);
            // 2）完整数据
            TemplateData templateData = new TemplateData(templates, templateRowRenderData);

            // 三、绑定插件
            // 1）插件绑定-【TemplateTableRenderPolicy12】插件中data能获取到【TemplateRowRenderData12】动态数据的关键
            // 注意：模板中也是根据该字段进行渲染：templateRowRenderData 如图resources/pictures/images1.jpg   特别注意不要用到了中文花括号{}，鼠鼠我郁闷了一早上也想不明道插件为什么不生效
            Configure config = Configure.builder().bind("templateRowRenderData", new TemplateTableRenderPolicy()).build();

            try (InputStream templateStream = new ClassPathResource(TEMPLATE_PATH).getInputStream(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
                XWPFTemplate template = XWPFTemplate.compile(templateStream, config).render(templateData);
                Assertions.assertNotNull(template);
                // 写入到byteArrayOutputStream
                template.write(byteArrayOutputStream);
                // 输出流【byteArrayOutputStream】转为输入流【ByteArrayInputStream】
                try (XWPFDocument xwpfDocument = new XWPFDocument(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()))) {
                    // 输出为docx
                    xwpfDocument.write(Files.newOutputStream(Paths.get(OUTPUT_DOCX_PATH)));
                    // 输出为PDF
                    PdfConverter.getInstance().convert(xwpfDocument, Files.newOutputStream(Paths.get(OUTPUT_PDF_PATH)), PdfOptions.create());
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    private Templates initTemplates() {
        // 填充一些基本信息
        Templates templates = new Templates();
        templates.setName("牧羊人OVO");
        templates.setSexName("未知");
        return templates;
    }

    /**
     * 测试数据、实际是需要查询数据库的
     *
     * @return
     */
    private List<DataValue> initFamilyMember() {
        List<DataValue> familyMemberList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            DataValue familyMember = new DataValue();
            familyMember.setTitle("自我评价");
            familyMember.setSubTitle("姓名" + i);
            familyMember.setContent("职位" + i);
            familyMemberList.add(familyMember);
        }
        for (int i = 0; i < 1; i++) {
            DataValue familyMember = new DataValue();
            familyMember.setTitle("公司");
            familyMember.setSubTitle("姓名" + i);
            familyMember.setContent("职位" + i);
            familyMemberList.add(familyMember);
        }
        for (int i = 0; i < 4; i++) {
            DataValue familyMember = new DataValue();
            familyMember.setTitle("其他");
            familyMember.setSubTitle("姓名" + i);
            familyMember.setContent("职位" + i);
            familyMemberList.add(familyMember);
        }


        return familyMemberList;
    }

    /**
     * 测试数据
     *
     * @return
     */
    private List<Going> initGoing() {
        List<Going> goingList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            Going going = new Going();
            going.setCompany("工作单位" + i);
            going.setAddress("地址" + i);
            going.setPhone("程联系电话" + i);
            goingList.add(going);
        }
        return goingList;
    }
}
